﻿using Direct.IDAL.User.Money;
using DirectSell.Common.CodeHelp;
using DirectSell.Common.Filter;
using DirectSell.Common.Page;
using DirectSell.Common.Session;
using DirectSell.Common.Url;
using DirectSell.IBLL;
using DirectSell.Model.Money;
using DirectSell.Model.User;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;

namespace DirectSell.WebLayer.Areas.User.Controllers
{
    /// <summary>
    /// 申请提现
    /// </summary>
    public class RealMoneyController : Controller
    {
        #region 注入

        [Dependency("RealMoney")]
        protected IOperation _service { get; set; }

        [Dependency]
        protected IRealMoneyRepository _repository { get; set; }
        #endregion

        #region 首页
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        [LoginFilter(IsCheck =true)]
        public async Task<ActionResult> Index(int page=1)
        {
            var sessioninfo = SessionInfo.GetSession();

            var pagedList = await _repository.Record(sessioninfo.UserCode,page,CommonConfig.PageSize);

            var Charge = await _repository.QueryServiceCharge();

            ViewBag.balance = QueryBalance();

            ViewBag.Charge = Math.Round(Charge*100, 0);

            return View(pagedList);
        }
        #endregion

        #region 申请提现
        /// <summary>
        /// 电子币申请
        /// </summary>
        /// <returns></returns>
        public async Task<UserRespCode> Apply(RealMoneyReq req)
        {
            //判断验证码是否正确
            if (SessionHelper.GetSession("code1") == null || req.CheckCode != SessionHelper.GetSession("code1").ToString())
            {
                //验证码错误
                return UserRespCode.验证码错误;
            }
            var sessioninfo = SessionInfo.GetSession();
            req.UserCode = sessioninfo.UserCode;
            return await _service.DoOperationAsync(req);
        }
        #endregion

        #region 获取图片验证码
        /// <summary>
        /// 获取图片验证码 
        /// </summary>
        /// <returns></returns>
        public ActionResult GetImageValiDateCode()
        {
            //获得验证码
            string code = ValidateCode.GetRandNumber(4);
            byte[] bytes = ValidateCode.CreateCheckCode(code);
            //验证码写入Session
            SessionHelper.SetSession("code1", code);
            return File(bytes, @"image/jpeg");
        }
        #endregion

        #region 查询实际到账
        /// <summary>
        /// 查询实际到账
        /// </summary>
        /// <returns></returns>
        public async Task<decimal> QueryCharge(decimal money)
        {
            var realmoney =  await _repository.Compute(money);
            return Math.Round(realmoney, 2);
        }
        #endregion

        #region 查余额
        /// <summary>
        /// 查余额
        /// </summary>
        /// <returns></returns>
        public decimal QueryBalance()
        {
            var usercode = SessionInfo.GetSession().UserCode;
            var balance = _repository.QueryBalance(usercode);
            return Math.Round(balance, 2);
        }
        #endregion
    }
}